package com.woniu.service.impl;

import com.woniu.dao.ICapitalChannelDao;
import com.woniu.entity.*;
import com.woniu.service.IAssetChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class IAssetChannelServiceImpl implements IAssetChannelService {
    @Autowired
    ICapitalChannelDao capitalChannelDao;

    @Override
    public List<CapitalChannelEntity> findAllAssetChannel() {

        List<CapitalChannelEntity> allAssetChannel = capitalChannelDao.findAllAssetChannel();
        List<Integer> productIds = new ArrayList<>();

        allAssetChannel.forEach((e)->{
            productIds.add(e.getId());
        });
        List<EnhancedProduction> allAssetChannelProducts = capitalChannelDao.findAllAssetChannelProducts(productIds);
        for (int i = 0; i < allAssetChannel.size(); i++) {
            allAssetChannel.get(i).getProducts().add(allAssetChannelProducts.get(i));
            allAssetChannel.get(i).setProductId(allAssetChannelProducts.get(i).getProductionId());
        }
        return allAssetChannel;
    }

    @Override
    public List<CapitalChannelEntity> selectChannels(CapitalChannelExportVO data) {
        List<CapitalChannelEntity> allAssetChannel = capitalChannelDao.findSpecificAssetChannel(data);
        List<Integer> productIds = new ArrayList<>();

        allAssetChannel.forEach((e)->{
            productIds.add(e.getId());
        });
        List<EnhancedProduction> allAssetChannelProducts = capitalChannelDao.findAllAssetChannelProducts(productIds);
        for (int i = 0; i < allAssetChannel.size(); i++) {
            allAssetChannel.get(i).getProducts().add(allAssetChannelProducts.get(i));
            allAssetChannel.get(i).setProductId(allAssetChannelProducts.get(i).getProductionId());
        }
        return allAssetChannel;
    }

    @Override
    public List<CapitalChannelEntity> searchWithLimits(CapitalChannelSearchVO data) {
        return capitalChannelDao.searchWithLimitsOfAsset(data);
    }

    @Override
    public List<ProductionInfo> listCapitalProductions() {
        return capitalChannelDao.listCapitalProductions();
    }

    @Override
    public Boolean addProductToChannel(Map<String, Object> data) {
        Integer isExist = capitalChannelDao.selectChannelProduct(data);
        if (isExist == 0) {
            Integer affectedRows = capitalChannelDao.addProductToChannel(data);
            if (affectedRows > 0) {
                return true;
            }
        }
        return false;

    }

    @Override
    public List<ProductionInfo> getAllRelativeProducts(Integer channelId) {
        return capitalChannelDao.getAllRelativeProducts(channelId);
    }

    @Override
    public Boolean deleteRelProduct(Integer cpmId) {
        Integer affectedRows = capitalChannelDao.deleteRelProduct(cpmId);
        if (affectedRows>0){
            return true;
        }
        return false;
    }

    @Override
    public List<ProductionInfo> searchRelProduct(Map<String, Object> data) {
        return capitalChannelDao.searchRelProduct(data);
    }

    @Override
    public Boolean addAssetChannel(CapitalChannelEntity data) {
        Integer count = capitalChannelDao.searchOneCapitalChannel(data.getChannelName());
        if (count>1){
            return false;
        }else{
            Integer maxCapitalChannelId = capitalChannelDao.getMaxCapitalChannelId();
            if (Objects.nonNull(maxCapitalChannelId)){
                data.setId(maxCapitalChannelId+1);
            }

            Integer add = capitalChannelDao.addAssetChannel(data);
            if (add==0){
                return false;
            }
            return true;
        }
    }
}
